/** @file bsp_key.c
*   @brief 
*   @par
*   COPYRIGHT: (c) 
*/

#include "./bsp_key.h"

static KeyStatus g_keyStatus;
/* @name
*  @brief
*  @parameter
*  @return
*/
KeyStatus GetKeyStatus (KeyOperation * key)
{
    KeyStatus  keyStatus = KEY_IDLE;
    static uint32_t keyPressMoment = 0;

    switch (g_keyStatus)
    {
        case KEY_IDLE:
            if (key->keyInfor.keyPressStatus == key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
            {
                g_keyStatus = KEY_CHECK;
                keyPressMoment = key->pfGetSystemTick();
            }
        break;

        case KEY_CHECK:
            if (key->pfGetSystemTick() - keyPressMoment >= 10)
            {
                if (key->keyInfor.keyPressStatus == key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
                {
                    g_keyStatus = KEY_SHORT_PRESS;
                } 
                else
                {
                    g_keyStatus = KEY_IDLE;
                    keyPressMoment = 0;
                }
            }
        break;

        case KEY_SHORT_PRESS:
            if (key->pfGetSystemTick() - keyPressMoment >= 150)
            {
                if (key->keyInfor.keyPressStatus == key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
                {
                    g_keyStatus = KEY_CHECK_LONG_PRESS;
                } 
                else
                {
                    g_keyStatus = KEY_CHECK_DOUBLE_PRESS;
                }

                keyPressMoment = key->pfGetSystemTick();
            }
        break;

        case KEY_CHECK_DOUBLE_PRESS:
            if (key->pfGetSystemTick() - keyPressMoment <= 150)
            {
                if (key->keyInfor.keyPressStatus == key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
                {
                    g_keyStatus = KEY_DOUBLE_PRESS;
                } 
            }
            else
            {
                g_keyStatus = KEY_IDLE;
                keyStatus = KEY_SHORT_PRESS;
            }

        break;

        case KEY_DOUBLE_PRESS:
            if (key->keyInfor.keyPressStatus != key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
            {
                g_keyStatus = KEY_IDLE;
                keyStatus = KEY_DOUBLE_PRESS;
            }
        break;

        case KEY_CHECK_LONG_PRESS:
            if (key->pfGetSystemTick() - keyPressMoment >= 500)
            {
                if (key->keyInfor.keyPressStatus == key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
                {
                    g_keyStatus = KEY_LONG_PRESS;
                } 
                else
                {
                    g_keyStatus = KEY_IDLE;
                    keyStatus = KEY_SHORT_PRESS;
                }  
            }          
        break;

        case KEY_LONG_PRESS:
            if (key->keyInfor.keyPressStatus != key->pfReadKeyPin(key->keyInfor.gpioPort, key->keyInfor.gpioPin))
            {
                g_keyStatus = KEY_IDLE;
                keyStatus = KEY_LONG_PRESS;
            }
        break;

        default:
        break;

    }

    return keyStatus;
}
